﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using K.Contract;
using K.Service;

namespace K.ClientPublic
{
    /// <summary>
    /// 客户端异常处理类
    /// </summary>
    public static class ExceptionPublic
    {
        //实例化一个静态的IExceptionDispose实例
        private static ServiceFactory<IExceptionDispose> _ExceptionDispose = new ServiceFactory<IExceptionDispose>();

        //静态属性，返回实现ExceptionDispose接口的服务实例
        public static IExceptionDispose ExceptionDispose
        {
            get 
            {
                return _ExceptionDispose.Service;
            }
        }

        /// <summary>
        /// 初始化客户端异常处理静态类
        /// </summary>
        /// <param name="factory"></param>
        public static void Initialize(ServiceFactoryDelegate<IExceptionDispose> factory)
        {
            _ExceptionDispose.Initialize(factory);
        }

        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="exp">异常</param>
        /// <param name="clientExceptionMethod">处理异常的委托</param>
        public static void ClientExceptionProcess(Exception exp, ClientExceptionProcessDelegate clientExceptionMethod)
        {
            //这里的异常应该分为两类；1类是客户端自己产生的异常；2类是服务异常而导致的异常

            if (exp is FaultException) //服务异常
            {
                FaultException NewFaultException = (FaultException)exp;
                //NewFaultException.d
            }
            else //非服务异常
            {
                clientExceptionMethod(exp);
                ChannelFactory<IExceptionDispose> chanelFactory = new ChannelFactory<IExceptionDispose>();
                IExceptionDispose i = chanelFactory.CreateChannel();
                
            }


            
        }


    }
}
